package controllers;

import java.util.ArrayList;
import java.util.List;

import models.Url;
import models.UserUrls;
import play.mvc.With;

@With(AuthorityFilter.class)
public class Application extends BaseController {


    public static void index() {
        String currentUserId = getCurrentUserid();
        List<UserUrls> userUrls = UserUrls.find("userId", Long.valueOf(currentUserId)).fetch();
        if (userUrls.isEmpty()) {
            render("/index.html");
        }
        
        List<Long> urlIds = new ArrayList<Long>();
        for (UserUrls u : userUrls) {
            urlIds.add(u.urlId);
        }
        
        StringBuilder hql = new StringBuilder(" id in (");
        for (int i = 0; i < urlIds.size(); i++) {
            hql.append(urlIds.get(i));
            if (i !=  urlIds.size() - 1) {
                hql.append(",");
            }
        }
        hql.append(") order by position");
        List<Url> urls = Url.find(hql.toString()).fetch();
        render("/index.html", urls);
    }
}
